package com.perritosca.webservice;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.perritosca.webservice.R;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.widget.Toast;

public class Utils {
	/** TEMP BORRAR **/
	public static final String MOBIAXIS_POLIZA1 = "mobiAXIS.poliza1";
	
	/** SCREENS **/
	public static final String SCR_PRODUCT_DETAIL = ".ProductDetailActivity";
	public static final String SCR_MAIN = ".MainActivity";
	public static final String SCR_ACCESS = ".AccessActivity";
	public static final String SCR_MESSAGE = ".MessageActivity";
	public static final String SCR_REGISTER = ".RegisterActivity";
	public static final String SCR_PHONE_LIST = ".PhoneListActivity";
	public static final String SCR_POLICIES = ".PoliciesActivity";
	public static final String SCR_SINISTER = ".SinisterActivity";
	public static final String SCR_STEPS = ".StepsActivity";
	public static final String SCR_BILLS = ".BillsActivity";
	public static final String SCR_PLACES = ".PlacesActivity";
	public static final String SCR_FAQS = ".FAQsActivity";
	public static final String SCR_PARKING = ".ParkingActivity";
	
	/** PARKING **/
	public static final String PARKING_LAT = "parking.lat";
	public static final String PARKING_LNG = "parking.lng";
	public static final String PARKING_OTROS = "parking.otros";
	
	/** WEBSERVICES **/
	public static final String URL_REGISTER = "http://curso.sperales.info/";
	public static final String URL_WEBSERVICE = "http://acces1.csi-ti.com/MOBILEiAXISListener/";
	
	/** INTERNAL USE **/
	public static final String USER_ID = "mobiAXIS.SPERSON";
	public static final String EMPRESA_ID = "mobiAXIS.EMPRESAID";
	
	/** MAPS **/
	public static final int MAP_ZOOM = 16;
	public static final int MAP_ZOOM_GENERAL = 4;
	
	/** IMAGE & PDF **/
	public static final String pathStorage = Environment.getExternalStorageDirectory() + "/Download/";
	
	
	public static String encryptPassword(String pwd){
		
		try {
			// Create MD5 Hash
			MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
			digest.update(pwd.getBytes());
			byte messageDigest[] = digest.digest();
			
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			for (int i=0; i<messageDigest.length; i++)
				hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
			return hexString.toString();
		} 
		catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}    
		return "";
	}
	public static void showMessage(Context context, String texto){
		CharSequence text = texto;
		int duration = Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
		
	}
	public static boolean isNetworkAvailable(Context context) {     
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);   
    	boolean retVal=false;
    	if (connectivity == null) { 
    		Utils.showMessage(context, context.getString(R.string.error_connection));   
    	} 
    	else {      
    		NetworkInfo[] info = connectivity.getAllNetworkInfo();      
    		if (info != null) {         
    			for (int i = 0; i < info.length; i++) {            
    				if (info[i].getState() == NetworkInfo.State.CONNECTED) {               
    					retVal=true;            
    				}         
    			}      
    		}   
    	}  
    	if (retVal==false){
    		Utils.showMessage(context, context.getString(R.string.error_connection));   
    	}
    	return retVal;
    }
	
	public static boolean gotoMain(Context ctx){
		ctx.startActivity(new Intent(SCR_MAIN));
		return true;
	}
	
	public static boolean gotoAccess(Context ctx){
		ctx.startActivity(new Intent(SCR_ACCESS));
		return true;
	}
	
	public static boolean gotoMessage(Context ctx){
		ctx.startActivity(new Intent(SCR_MESSAGE));
		return true;
	}
	
	public static boolean gotoRegister(Context ctx){
		ctx.startActivity(new Intent(SCR_REGISTER));
		return true;
	}
	
	public static boolean gotoPhoneList(Context ctx){
		ctx.startActivity(new Intent(SCR_PHONE_LIST));
		return true;
	}
	
	public static boolean gotoPolicies(Context ctx){
		ctx.startActivity(new Intent(SCR_POLICIES));
		return true;
	}
	
	public static boolean gotoAddSinister(Context ctx){
		ctx.startActivity(new Intent(SCR_SINISTER));
		return true;
	}
	
	public static boolean gotoSteps(Context ctx){
		ctx.startActivity(new Intent(SCR_STEPS));
		return true;
	}
	
	public static boolean gotoBills(Context ctx){
		ctx.startActivity(new Intent(SCR_BILLS));
		return true;
	}
	
	public static boolean gotoPlaces(Context ctx){
		ctx.startActivity(new Intent(SCR_PLACES));
		return true;
	}
	
	public static boolean gotoFAQs(Context ctx){
		ctx.startActivity(new Intent(SCR_FAQS));
		return true;
	}
	
	public static boolean gotoParking(Context ctx){
		ctx.startActivity(new Intent(SCR_PARKING));
		return true;
	}
	
	public static int convertDpToPixel(float dp,Context context){
	    Resources resources = context.getResources();
	    DisplayMetrics metrics = resources.getDisplayMetrics();
	    float px = dp * (metrics.densityDpi/160f);
	    return Math.round(px);
	}
	public static void clickShare(Context ctx, String subject, String text, String title){
		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
		shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);

		ctx.startActivity(Intent.createChooser(shareIntent, title));
	}
}
